<?php

namespace App\Tools;

class Curl
{

  public static function postFenbi($url, $data)
  {
    $ch = curl_init();
    $post_fields = http_build_query($data);
    curl_setopt_array($ch, [
      CURLOPT_URL            => $url,
      CURLOPT_POST           => true,
      CURLOPT_POSTFIELDS     => $post_fields,
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_TIMEOUT        => 30,
      CURLOPT_HTTPHEADER     => [
        'Content-Type: application/x-www-form-urlencoded',
        'Content-Length: ' . strlen($post_fields),
      ],
      CURLOPT_USERAGENT      => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36 Edg/142.0.0.0',
      CURLOPT_COOKIE         => 'Hm_lvt_e7351028cde0d0ccb9ccdbe5fe531683=1763390618; HMACCOUNT=5E20182B02415C49; userid=161114059; sess=fSkuaBWCUcr0L8VwEFQUT/48UkmdS3L5ENN9pg4/XvrY20zfN0JQDL8aMI7yKlXkmKj+vsBEOv+C8SkZDt3scdw3vKni9BvWI1lyxbA5P1A=; sid=661142; persistent=9FphRgQ1pO3CrjHpE7NWYslmvXR4+LECeZGm2tnRF2TrBInsSf4cjguASTl31aqu8VUUnTK1mkRFNxen4KyA6g==; sensorsdata2015jssdkcross=%7B%22distinct_id%22%3A%22161114059%22%2C%22first_id%22%3A%2219a92457887257-0d8008fd66c04c8-4c657b58-1440000-19a924578891bc5%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E8%87%AA%E7%84%B6%E6%90%9C%E7%B4%A2%E6%B5%81%E9%87%8F%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC%22%2C%22%24latest_referrer%22%3A%22https%3A%2F%2Fcn.bing.com%2F%22%7D%2C%22identities%22%3A%22eyIkaWRlbnRpdHlfY29va2llX2lkIjoiMTlhOTI0NTc4ODcyNTctMGQ4MDA4ZmQ2NmMwNGM4LTRjNjU3YjU4LTE0NDAwMDAtMTlhOTI0NTc4ODkxYmM1IiwiJGlkZW50aXR5X2xvZ2luX2lkIjoiMTYxMTE0MDU5In0%3D%22%2C%22history_login_id%22%3A%7B%22name%22%3A%22%24identity_login_id%22%2C%22value%22%3A%22161114059%22%7D%2C%22%24device_id%22%3A%2219a92457887257-0d8008fd66c04c8-4c657b58-1440000-19a924578891bc5%22%7D; Hm_lpvt_e7351028cde0d0ccb9ccdbe5fe531683=1763391195; acw_tc=0bdd344a17634005602087503ee070a5f91a17b0c970c653c3f255eec5325f',
      CURLOPT_SSL_VERIFYPEER => false,
      CURLOPT_SSL_VERIFYHOST => 2,
    ]);
    $response = curl_exec($ch);
    if (curl_errno($ch)) {
      $error_msg = curl_error($ch);
      $error_num = curl_errno($ch);
      echo "cURL 底层错误: {$error_num} - {$error_msg}\n";
      $response = false;
    }
    // 6. 如果响应为空，获取详细信息
    if ($response === '' || $response === false) {
      $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
      echo "请求 URL: {$url}\n";
      echo "发送数据: {$post_fields}\n";
      echo "HTTP 状态码: {$http_code}\n";
      if ($http_code >= 400) {
        echo "服务器返回错误状态码，但响应体为空。检查服务器日志。\n";
      } elseif ($http_code === 0) {
        echo "HTTP 状态码为 0，这通常表示连接失败或超时。\n";
      } else {
        echo "HTTP 状态码为 {$http_code} (例如 200/204)，但响应体为空。服务器可能故意返回空内容或数据未被接收。\n";
      }
    }

    curl_close($ch);

    return $response;
  }
  public static function getFenbi($url)
  {
    $ch = curl_init();

    curl_setopt_array($ch, [
      CURLOPT_URL            => $url,
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_TIMEOUT        => 30,
      CURLOPT_HTTPHEADER     => [],
      CURLOPT_USERAGENT      => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36 Edg/142.0.0.0',
      CURLOPT_COOKIE         => 'Hm_lvt_e7351028cde0d0ccb9ccdbe5fe531683=1763390618; HMACCOUNT=5E20182B02415C49; userid=161114059; sess=fSkuaBWCUcr0L8VwEFQUT/48UkmdS3L5ENN9pg4/XvrY20zfN0JQDL8aMI7yKlXkmKj+vsBEOv+C8SkZDt3scdw3vKni9BvWI1lyxbA5P1A=; sid=661142; persistent=9FphRgQ1pO3CrjHpE7NWYslmvXR4+LECeZGm2tnRF2TrBInsSf4cjguASTl31aqu8VUUnTK1mkRFNxen4KyA6g==; sensorsdata2015jssdkcross=%7B%22distinct_id%22%3A%22161114059%22%2C%22first_id%22%3A%2219a92457887257-0d8008fd66c04c8-4c657b58-1440000-19a924578891bc5%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E8%87%AA%E7%84%B6%E6%90%9C%E7%B4%A2%E6%B5%81%E9%87%8F%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC%22%2C%22%24latest_referrer%22%3A%22https%3A%2F%2Fcn.bing.com%2F%22%7D%2C%22identities%22%3A%22eyIkaWRlbnRpdHlfY29va2llX2lkIjoiMTlhOTI0NTc4ODcyNTctMGQ4MDA4ZmQ2NmMwNGM4LTRjNjU3YjU4LTE0NDAwMDAtMTlhOTI0NTc4ODkxYmM1IiwiJGlkZW50aXR5X2xvZ2luX2lkIjoiMTYxMTE0MDU5In0%3D%22%2C%22history_login_id%22%3A%7B%22name%22%3A%22%24identity_login_id%22%2C%22value%22%3A%22161114059%22%7D%2C%22%24device_id%22%3A%2219a92457887257-0d8008fd66c04c8-4c657b58-1440000-19a924578891bc5%22%7D; Hm_lpvt_e7351028cde0d0ccb9ccdbe5fe531683=1763391195; acw_tc=0bdd344a17634005602087503ee070a5f91a17b0c970c653c3f255eec5325f',
      CURLOPT_SSL_VERIFYPEER => false,
      CURLOPT_SSL_VERIFYHOST => 2,
    ]);
    $response = curl_exec($ch);
    if (curl_errno($ch)) {
      $error_msg = curl_error($ch);
      $error_num = curl_errno($ch);
      echo "cURL 底层错误: {$error_num} - {$error_msg}\n";
      $response = false;
    }
    // 6. 如果响应为空，获取详细信息
    if ($response === '' || $response === false) {
      $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
      echo "请求 URL: {$url}\n";
      echo "HTTP 状态码: {$http_code}\n";
      if ($http_code >= 400) {
        echo "服务器返回错误状态码，但响应体为空。检查服务器日志。\n";
      } elseif ($http_code === 0) {
        echo "HTTP 状态码为 0，这通常表示连接失败或超时。\n";
      } else {
        echo "HTTP 状态码为 {$http_code} (例如 200/204)，但响应体为空。服务器可能故意返回空内容或数据未被接收。\n";
      }
    }

    curl_close($ch);

    return $response;
  }
}
